//
//  BindingEmailController.m
//  OnionHome
//
//  Created by fengjie on 2019/9/13.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "BindingEmailController.h"

@interface BindingEmailController ()

/**邮箱*/
@property (nonatomic,strong)UITextField*emailField;
/**密码*/
@property (nonatomic,strong)UITextField*pawField;
/**协议点击文字*/
@property (nonatomic,strong)AgreementView*agreementView;
/**登录按钮*/
@property (nonatomic,strong)UIButton*loginBtn;

@end

@implementation BindingEmailController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName :UIColorFromRGB(0x24B34A), NSFontAttributeName : SYSTEMFONT(19)}];
    self.title = @"绑定邮箱";
    [self createUI];
    [self setNotificaAction];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setNavBarBackgroundAndClearColor];
}

-(void)setNotificaAction{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(chaneTextFiel) name:UITextFieldTextDidChangeNotification object:nil];
}
-(void)chaneTextFiel{
    if (self.emailField.text.length > 0 && self.pawField.text.length >0 && self.agreementView.isSelected) {
        self.loginBtn.enabled = YES;
    }else{
        self.loginBtn.enabled = NO;
    }
}
-(void)createUI{
    
    UIView * oneLine = UIView.new;
    oneLine.backgroundColor = UIColorFromRGB(0xE9EEE7);
    
    UIView * twoLine = UIView.new;
    twoLine.backgroundColor = UIColorFromRGB(0xE9EEE7);
    
    
    [self.view addSubview:self.emailField];
    [self.view addSubview:self.pawField];
    [self.view addSubview:oneLine];
    [self.view addSubview:twoLine];
    [self.view addSubview:self.agreementView];
    [self.view addSubview:self.loginBtn];
    
    [self.emailField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(45);
        make.right.mas_equalTo(-45);
        make.top.mas_equalTo(210);
        make.height.mas_equalTo(40);
    }];
    
    [oneLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.emailField);
        make.top.mas_equalTo(self.emailField.mas_bottom);
        make.height.mas_equalTo(0.5);
    }];
    
    [self.pawField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(45);
        make.right.mas_equalTo(-45);
        make.top.mas_equalTo(self.emailField.mas_bottom).offset(10);
        make.height.mas_equalTo(40);
    }];
    [twoLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.emailField);
        make.top.mas_equalTo(self.pawField.mas_bottom);
        make.height.mas_equalTo(0.5);
    }];
    [self.agreementView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(55);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(twoLine.mas_bottom).offset(55);
    }];
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(45);
        make.right.mas_equalTo(-45);
        make.top.mas_equalTo(self.agreementView.mas_bottom).offset(15);
        make.height.mas_equalTo(40);
    }];
    
    
}
#pragma mark 懒加载

-(UITextField*)emailField{
    if(!_emailField){
        _emailField=[UITextField new];
        _emailField.placeholder = @"输入邮箱地址";
        _emailField.keyboardType = UIKeyboardTypeEmailAddress;
    }
    return _emailField;
}
-(UITextField*)pawField{
    if(!_pawField){
        _pawField=[UITextField new];
        _pawField.placeholder =@"输入密码";
    }
    return _pawField;
}
-(AgreementView*)agreementView{
    if(!_agreementView){
        _agreementView=[[AgreementView alloc] initWithFrame:CGRectZero];
        kWeakSelf(weakSelf);
        _agreementView.touchHandler = ^{
            [weakSelf chaneTextFiel];
        };
        
    }
    return _agreementView;
}

-(UIButton*)loginBtn{
    if(!_loginBtn){
        _loginBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [_loginBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_loginBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        [_loginBtn setBackgroundImage:[UIImage createImageWithColor:UIColorFromRGB(0x9ADCAB)] forState:UIControlStateDisabled];
        [_loginBtn setBackgroundImage:[UIImage createImageWithColor:UIColorFromRGB(0x24B34A)] forState:UIControlStateNormal];
        _loginBtn.enabled = NO;
        
    }
    return _loginBtn;
}

@end
